const log4js = require('koa-log4');
const logger = log4js.getLogger('routes::staff');
var querystring = require('querystring');
const router = require('koa-router')();
exports = module.exports = router;
const mongodb = require('mongodb');
const ObjectId = mongodb.ObjectId;

const _ = require('lodash');
const util = require(global.prefixPath + '/commons/util');
const site_service = require(global.prefixPath + '/services/site');
const info_service = require(global.prefixPath + '/services/info');
const city_service = require(global.prefixPath + '/services/city');
const ad_service = require(global.prefixPath + '/services/adsense');

router.get('/:city/index', async (ctx, next) => {
	 let params = querystring.parse(ctx.req._parsedUrl.query);
	 let time = params.query?params.query.time:'3';
	 let now = new Date();
	 let date = (time == '更多'?0:new Date(now.getTime() - time * 24 * 3600 * 1000));
	 let q = { // 最新
		   query: {
			   infoSource: 'TENGPU',
			   createdAt: {
				   '$gte': date,
				   '$lte': now
			   }
		   },
		   sort: {
			 createdAt: -1
		 },
		 page: {
			 index: 1,
			 size: 6
		 }
	 }
	 let lovea = { // 优选 isPreference
		 query: {
			 infoSource: 'TENGPU',
			 infoType: 'Attorn',
			 isPreference: true
		 },
		 sort: {
		   createdAt: -1
	   },
	   page: {
		   index: 1,
		   size: 5
	   }
	 }
	 let loves = { // 优选 isPreference
		 query: {
			 infoSource: 'TENGPU',
			 infoType: 'SeekLease',
			 isPreference: true
		 },
		 sort: {
		   createdAt: -1
	   },
	   page: {
		   index: 1,
		   size: 5
	   }
	 }
	 let seven = { // 优选 isPreference
		 query: {
			//  infoSource: 'TENGPU',
			 createdAt: {
				 '$gte': new Date(now.getTime() - 7 * 24 * 3600 * 1000),
				 '$lte': now
			 }
		 },
		 sort: {
		   createdAt: -1
	   },
	   page: {
		   index: 1,
		   size: 6
	   }
	 }
	 let deal = { // 优选 isPreference
		 query: {
			//  infoSource: 'TENGPU',
			 isTraded: "true"
		 },
		 sort: {
		   createdAt: -1
	   },
	   page: {
		   index: 1,
		   size: 10
	   }
	 }
	 let seek = { // 优选 isPreference
		 query: {
			 infoSource: 'PERSONAL'
		 },
		 sort: {
		   createdAt: -1
	   },
	   page: {
		   index: 1,
		   size: 12
	   }
	 }
	 let deal_result = await info_service.find(deal);
	 let info_result = await info_service.find(q);
	 let lovea_result = await info_service.find(lovea);
	 let loves_result = await info_service.find(loves);
	 let seven_result = await info_service.find(seven);
	 let seek_result = await info_service.find(seek);
	 let ad_result = await ad_service.findAll();
	//  console.log(deal_result)
	 ctx.state.pub_info = info_result;
	 ctx.state.lovea_info = lovea_result;
	 ctx.state.loves_info = loves_result;
	 ctx.state.seven_info = seven_result;
	 ctx.state.deal_info = deal_result;
	 ctx.state.seek_info = seek_result;
	 ctx.state.ad_info = ad_result;
	 return await ctx.render('substation/index');
});
